package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.api.common.CommonProblemSelectDto;
import com.dog.basics.bean.model.common.SysCommonProblem;
import com.dog.basics.bean.vo.admin.sys.AdminCommonProblemDetailVo;
import com.dog.basics.bean.vo.api.common.CommonProblemAppPageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.SysCommonProblemMapper;
import com.dog.basics.service.common.ISysCommonProblemService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 常见问题 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-10-11
 */
@Service
@AllArgsConstructor
public class SysCommonProblemServiceImpl extends ServiceImpl<SysCommonProblemMapper, SysCommonProblem>
        implements ISysCommonProblemService {


    /**
     * 常见问题列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<CommonProblemAppPageVo> getCommonProblemPageList(PageParam page, CommonProblemSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblem>query().lambda()
                .eq(SysCommonProblem::getProblemClassId, dto.getClassId())
                .eq(SysCommonProblem::getProblemType, dto.getProblemType())
                .like(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblem::getTitle, dto.getTitle()));
        List<SysCommonProblem> record = page.getRecords();
        page.setRecords(record.stream().map(sysCommonProblem -> BeanUtil.toBean(sysCommonProblem, CommonProblemAppPageVo.class)).collect(Collectors.toList()));
        return page;
    }

    /**
     * 常见问题详情
     *
     * @param id
     * @return
     * @author guonh
     */
    @Override
    public AdminCommonProblemDetailVo getCommonProblemDetail(Long id) {
        SysCommonProblem byId = this.getById(id);
        Validator.validateNotNull(byId, "common.noData");
        return BeanUtil.toBean(byId, AdminCommonProblemDetailVo.class);

    }
}
